package com.cictec.middleware.gps.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.cache.ecache.EHCacheUtils;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.BusScheduleApply;
import com.cictec.middleware.gps.service.feign.MiddlewareDcFeignClient;

/**
 * 调度申请
 * 
 * @author Administrator
 *
 */
@Service
public class BusScheduleApplyService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private MiddlewareDcFeignClient middlewareDcFeignClient;

	/** 调度申请缓存*/
	private static String BUS_SCHEDULE_APPLY = "bus-schedule-apply";
	
	/**
	 * 保存调度申请
	 * @param busScheduleApply
	 */
	public void saveBusScheduleApply(BusScheduleApply busScheduleApply) {

		if (PMSUtils.isEmpty(busScheduleApply.getBsaUuid())) {
			busScheduleApply.setBsaUuid(PMSUtils.getUUID());
			middlewareDcFeignClient.saveBusScheduleApply(busScheduleApply);
		}
		
		try {
			// 删除缓存
			EHCacheUtils.removeElment(BUS_SCHEDULE_APPLY, busScheduleApply.getBsaBusUuid());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 是否是非运营调度申请
	 * @param busScheduleApply
	 * @return  true:非运营调度申请；false:运营调度申请
	 */
	public boolean isInNotOperateScheduleApplay(BusScheduleApply busScheduleApply){
		String notOperate = PropertiesUtils.getString("schedule.apply.feiyunying");
		String [] notarr = notOperate.split(",");
		for (int i = 0; i < notarr.length; i++) {
			
			if(notarr[i].equals(PMSUtils.isNull(busScheduleApply.getBsaType()))){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 【缓存】刷新调度申请缓存
	 */
	public void reloadBusScheduleApply(String busUuid, Date time){
		
		List<BusScheduleApply> list = selectBusScheduleApply(busUuid, time);
		EHCacheUtils.setValue(BUS_SCHEDULE_APPLY, busUuid, list);
	}
	
	/**
	 * 获取【最近一次】调度申请
	 * @param busUuid
	 * @param time 当前时间
	 */
	public BusScheduleApply getBusScheduleApply(String busUuid, Date time){

		List<BusScheduleApply> list = EHCacheUtils.getValue(BUS_SCHEDULE_APPLY, busUuid);
		
		if(list != null){
			return getBusScheduleApply(list, time);
		}
		
		list = selectBusScheduleApply(busUuid, time);
		EHCacheUtils.setValue(BUS_SCHEDULE_APPLY, busUuid, list);
		
		if(list.size() > 0){
			return getBusScheduleApply(list, time);
		}
		return null;
	}
	
	private BusScheduleApply getBusScheduleApply(List<BusScheduleApply> applyList, Date time){
		BusScheduleApply apply = null;
		
		for (BusScheduleApply busScheduleApply : applyList) {
			if(DateUtils.parseDateTimes(busScheduleApply.getBsaCreateTime()).getTime() <= time.getTime()){
				apply = busScheduleApply;
			}
		}
		return apply;
	}
	
	
	/**
	 * 获取指定日期 到当前时间 所有的调度申请
	 * @param busUuid
	 * @param time
	 * @return
	 */
	public List<BusScheduleApply> selectBusScheduleApply(String busUuid, Date time){
		List<BusScheduleApply> list =middlewareDcFeignClient.selectBusScheduleApply(busUuid, time);
		return list;
	}
	
	
}
